<template>
  <div class="directorys-edit">
    <el-form :model="directoryForm" :rules="directoryFormRules" ref="directoryFormRef">
      <el-form-item label="所属学科" label-width="80px" prop="subjectID" v-if="!subjectsId">
        <el-select v-model="directoryForm.subjectID" placeholder="请选择">
          <el-option
            :label="item.label"
            :value="item.value"
            v-for="(item,index) in simpleSubjectsList"
            :key="index"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="目录名称" label-width="80px" prop="directoryName">
        <el-input v-model="directoryForm.directoryName" autocomplete="off"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="$emit('close')">取 消</el-button>
      <el-button type="primary" @click="editDirectorys">确 定</el-button>
    </div>
  </div>
</template>

<script>
// 导入添加或修改目录请求的组件
import { add, update, detail } from "@/api/hmmm/directorys";
import { simpleSubjects } from "@/api/hmmm/subjects";
export default {
  name: "DirectorysEdit",
  data() {
    return {
      //   directoryForm: {}, // form表单数据
      directoryFormRules: {
        // 验证规则
        subjectID: [
          { required: true, message: "请选择学科名称", trigger: "change" },
        ],
        directoryName: [
          { required: true, message: "请输入目录名称", trigger: "blur" },
        ],
      },
      simpleSubjectsList: [],
    };
  },
  props: {
    userID: {
      type: [Number, String],
      required: true,
    },
    // directoryData: {
    //   type: Object,
    //   required: true,
    // },
    directoryForm: {
      type: Object,
      required: true,
    },
    subjectsId: {
      type: [Number, String],
    },
  },
  computed: {}, // 计算属性
  mounted() {
    this.getSimpleSubjects();
  },

  methods: {
    // 获取科学简单列表
    async getSimpleSubjects() {
      try {
        const { data: res } = await simpleSubjects();
        this.simpleSubjectsList = res;
      } catch (error) {
        this.$message.error("获取简单科学列表数据失败");
      }
    },
    // 重置表单
    resetForm() {
      // 重置表单数据
      this.$refs.directoryFormRef.resetFields();
    },
    // 修改目录请求
    editDirectorys() {
      console.log(this.directoryForm);
      // 表单预校验
      this.$refs.directoryFormRef.validate(async (valid) => {
        if (!valid) {
          return;
        }
        if (this.subjectsId) {
          this.directoryForm.subjectID = this.subjectsId;
        }
        try {
          await update(this.directoryForm);
          // 修改成功提示内容
          this.$message.success("修改成功");
          // 关闭弹框
          this.$emit("close");
          // 渲染列表 调用父组件上面的方法
          this.$parent.$parent.loadDirectoryList();
        } catch (error) {
          console.log(error);
          this.$message.error("修改失败");
        }
      });
    },
  },

  filters: {},
};
</script>

<style scoped lang='less'>
.el-select {
  width: 100%;
}
.dialog-footer {
  display: flex;
  justify-content: center;
}
</style>